package com.hikealoha.hikealoha;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hikealoha.hikealoha.R;

import java.util.ArrayList;
import java.util.List;

public class OptionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";
    private ArrayList<String> categoryList;
    private ListView categoryListView;

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static OptionFragment newInstance(int position) {
        OptionFragment fragment = new OptionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public OptionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_option, container, false);
        categoryListView = (ListView) rootView.findViewById(R.id.hike_list);
        categoryList = new ArrayList<>();
        String[] listCategories = new String[]{"Aihualama Falls", "Diamond Head", "Koko Head", "Manoa Falls", "Maunawili Falls"};

        for (int i = 0; i < listCategories.length; ++i) {
            categoryList.add(listCategories[i]);
        }
        //list.addAll(Arrays.asList(listCategories));
        ListViewAdapter listViewAdapter = new ListViewAdapter (getActivity(), categoryList);
        categoryListView.setAdapter(listViewAdapter);

        categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                    @Override
                                                    public void onItemClick(AdapterView<?> parent, final View view,
                                                                            int position, long id) {
                                                        final String item = (String) parent.getItemAtPosition(position);
                                                        Intent detailTrailIntent = new Intent(getActivity().getApplicationContext(), TrailDetailJava.class);
                                                        startActivity(detailTrailIntent);
                                                    }
                                                }
        );
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((HomeActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }

    public class ListViewAdapter extends BaseAdapter {
        LayoutInflater mLayoutInflater;
        List<String> mCategoryList;

        ListViewAdapter(Context context, List<String> categoryList){
            mLayoutInflater = LayoutInflater.from(context);
            this.mCategoryList = categoryList;

        }

        @Override
        public int getCount() {
            return mCategoryList.size();
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //LinearLayout categoryLayout = (LinearLayout)mLayoutInflater.inflate
            //(R.layout.categories_list, parent, false);
            LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.categories_list, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.list_item);
            textView.setText(mCategoryList.get(position));
            textView.setTextColor(Color.BLACK);
            textView.setTextSize(20);
            textView.setBackgroundColor(Color.WHITE);
            textView.setCompoundDrawablePadding(100);
            textView.setGravity(Gravity.CENTER_VERTICAL);
            switch (position) {
                case 0:
                    textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.aihualamafallsthumb, 0, 0 , 0);
                    break;
                case 1:
                    textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sampleimage1thumb, 0, 0 , 0);
                    break;
                case 2:
                    textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sampleimage2thumb, 0, 0 , 0);
                    break;
                case 3:
                    textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sampleimage3thumb, 0, 0 , 0);
                    break;
                case 4:
                    textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sampleimage4thumb, 0, 0 , 0);
                    break;
            }

            return rowView;
        }
    }
}